package org.jeecg.modules.xdbase.service.impl;

import org.jeecg.modules.xdbase.entity.FxjBase01;
import org.jeecg.modules.xdbase.entity.FxjBase0110;
import org.jeecg.modules.xdbase.entity.FxjBase0120;
import org.jeecg.modules.xdbase.entity.FxjBase0130;
import org.jeecg.modules.xdbase.entity.FxjBase0140;
import org.jeecg.modules.xdbase.mapper.FxjBase0110Mapper;
import org.jeecg.modules.xdbase.mapper.FxjBase0120Mapper;
import org.jeecg.modules.xdbase.mapper.FxjBase0130Mapper;
import org.jeecg.modules.xdbase.mapper.FxjBase0140Mapper;
import org.jeecg.modules.xdbase.mapper.FxjBase01Mapper;
import org.jeecg.modules.xdbase.service.IFxjBase01Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 产品管理
 * @Author: zhaodui
 * @Date:   2020-02-20
 * @Version: V1.0
 */
@Service
public class FxjBase01ServiceImpl extends ServiceImpl<FxjBase01Mapper, FxjBase01> implements IFxjBase01Service {

	@Autowired
	private FxjBase01Mapper fxjBase01Mapper;
	@Autowired
	private FxjBase0110Mapper fxjBase0110Mapper;
	@Autowired
	private FxjBase0120Mapper fxjBase0120Mapper;
	@Autowired
	private FxjBase0130Mapper fxjBase0130Mapper;
	@Autowired
	private FxjBase0140Mapper fxjBase0140Mapper;
	
	@Override
	@Transactional
	public void saveMain(FxjBase01 fxjBase01, List<FxjBase0110> fxjBase0110List,List<FxjBase0120> fxjBase0120List,List<FxjBase0130> fxjBase0130List,List<FxjBase0140> fxjBase0140List) {
		fxjBase01Mapper.insert(fxjBase01);
		if(fxjBase0110List!=null && fxjBase0110List.size()>0) {
			for(FxjBase0110 entity:fxjBase0110List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0110Mapper.insert(entity);
			}
		}
		if(fxjBase0120List!=null && fxjBase0120List.size()>0) {
			for(FxjBase0120 entity:fxjBase0120List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0120Mapper.insert(entity);
			}
		}
		if(fxjBase0130List!=null && fxjBase0130List.size()>0) {
			for(FxjBase0130 entity:fxjBase0130List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0130Mapper.insert(entity);
			}
		}
		if(fxjBase0140List!=null && fxjBase0140List.size()>0) {
			for(FxjBase0140 entity:fxjBase0140List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0140Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FxjBase01 fxjBase01,List<FxjBase0110> fxjBase0110List,List<FxjBase0120> fxjBase0120List,List<FxjBase0130> fxjBase0130List,List<FxjBase0140> fxjBase0140List) {
		fxjBase01Mapper.updateById(fxjBase01);
		
		//1.先删除子表数据
		fxjBase0110Mapper.deleteByMainId(fxjBase01.getId());
		fxjBase0120Mapper.deleteByMainId(fxjBase01.getId());
		fxjBase0130Mapper.deleteByMainId(fxjBase01.getId());
		fxjBase0140Mapper.deleteByMainId(fxjBase01.getId());
		
		//2.子表数据重新插入
		if(fxjBase0110List!=null && fxjBase0110List.size()>0) {
			for(FxjBase0110 entity:fxjBase0110List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0110Mapper.insert(entity);
			}
		}
		if(fxjBase0120List!=null && fxjBase0120List.size()>0) {
			for(FxjBase0120 entity:fxjBase0120List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0120Mapper.insert(entity);
			}
		}
		if(fxjBase0130List!=null && fxjBase0130List.size()>0) {
			for(FxjBase0130 entity:fxjBase0130List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0130Mapper.insert(entity);
			}
		}
		if(fxjBase0140List!=null && fxjBase0140List.size()>0) {
			for(FxjBase0140 entity:fxjBase0140List) {
				//外键设置
				entity.setQuery01(fxjBase01.getQuery01());
				fxjBase0140Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		fxjBase0110Mapper.deleteByMainId(id);
		fxjBase0120Mapper.deleteByMainId(id);
		fxjBase0130Mapper.deleteByMainId(id);
		fxjBase0140Mapper.deleteByMainId(id);
		fxjBase01Mapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fxjBase0110Mapper.deleteByMainId(id.toString());
			fxjBase0120Mapper.deleteByMainId(id.toString());
			fxjBase0130Mapper.deleteByMainId(id.toString());
			fxjBase0140Mapper.deleteByMainId(id.toString());
			fxjBase01Mapper.deleteById(id);
		}
	}
	
}
